#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "tinystr.h"
#include "tinyxml.h"
#include "Node.h"
#include "Edge.h"
#include <map>
#include <time.h>
#include <iterator>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <numeric>
using namespace std;
#include <algorithm>
double setDecimalpoint(double k)
{
	double t1;
	t1 = k - (int)k;  //取小数部分。
	t1 *= pow(10, 7); //小数点左移。
	if (t1 - (int)t1 >= 0.5)
		t1 += 1;	  //四舍五入
	t1 = (int)t1;	  //取整数部分。
	t1 /= pow(10, 7); //小数点右移。
	t1 += (int)k;	  //加上原本的整数部分。
	return t1;
	//return k;
}
template <typename T>
vector<size_t> sort_indexes_e(vector<T> &v)
{
	vector<size_t> idx(v.size());
	iota(idx.begin(), idx.end(), 0);
	sort(idx.begin(), idx.end(),
		 [&v](size_t i1, size_t i2)
		 { return v[i1] < v[i2]; });
	return idx;
}
static int sccc = 0;

struct _Path
{
	double cost[2];
	vector<int> vpaths[2];
	vector<string> edges;
	double distance;
	int weight = 0;
	void print()
	{
		//distance = cost[0] + cost[1];
	//	printf("d:%.8f m  p:", distance);

		 for (auto &it : vpaths[0])
		 {
			cout << it << "-";
		 }
		 //cout << endl<<"edge:";
		//calEdge(graph);
		for (auto &it : edges)
		{
			//cout << it <<" ";
		}
		cout << endl;
	}
	size_t calEdge(map<string, double> &allei)
	{
		if (edges.size() == 0)
		{
			distance = 0;
			for (size_t i = vpaths[0].size() - 1; i >= 1; i--)
			{
				string name(to_string(vpaths[0][i]) + "-" + to_string(vpaths[0][i - 1]));
				distance += allei[name];
				edges.push_back(name);
			}
		}
		// vector<string>().swap(edges); //清除容器并最小化它的容量，
		return edges.size();
	}
};

//两个vector求交集
vector<string> vectors_intersection(vector<string> v1, vector<string> v2) {
	vector<string> v;
	sort(v1.begin(), v1.end());
	sort(v2.begin(), v2.end());
	set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v));//求交集 
	return v;
}




class Graph
{
public:
	vector<_Path> paths;

	//map<string, Edge> edges;
	//map<string, Node> nodes;
	vector<Edge> vedges;
	vector<int> new_path;
	map<string,double> allEdgeswight;
	Node *pnodes;
	int *edgemap1;
	int *edgemap2;
	double *weight;
	vector<int> repect_point;
	int sum = 0;
	int edge_max_num = 0;
	//vector<Node> vnodes;
	vector<Node> vnodes;
	list<Node> listnode;
	static int start;
	static int end;
	int is_in_nodes(int index)
	{
		for (int V = 0; V <edge_max_num ; V++)
		{
			Node* p1 = &pnodes[edgemap1[V]];//V为边序号ID graph.edgemap1[V]值为节点ID
			//Node* p2 = &pnodes[edgemap2[V]];
			if (index == p1->index)
				return V;
			/*if(p1->index)*/
			//graph.weight[V] = p1->GetDistance(p2);
			//string name(to_string(graph.edgemap1[V]) + "-" + to_string(graph.edgemap2[V]));
			//graph.allEdgeswight[name] = graph.weight[V];
		}
		cout << endl;
		return -1;
	}

	 bool calChognhe(_Path &pa)// vector<int> p1, vector<vector<int>>& p2)
	{
		vector<double> f;
		pa.calEdge(allEdgeswight);
		pa.print();
		//copy(v.begin(), v.end(), ostream_iterator<string>(cout, " ")); cout << "vpaths[0]" << endl;
	
		for (int k = 0; k < paths.size(); k++)
		{
			double sum = 0;
			for (int j = 0; j < pa.edges.size(); j++)
			{
				for (int i = 0; i < paths[k].edges.size(); i++)
				{
					if (pa.edges[j] == paths[k].edges[i])
						sum += allEdgeswight[pa.edges[j]];
				}
		
			}
			f.push_back((sum) / (paths[k].distance));
	/*		cout <<"sum="<< sum << endl;*/
		}
	

		//copy(paths[0].vpaths[0].begin(), paths[0].vpaths[0].end(), ostream_iterator<int>(cout, " ")); cout << "vpaths[0]" << endl;
		//copy(pa.vpaths[0].begin(), pa.vpaths[0].end(), ostream_iterator<int>(cout, " ")); cout << "pa" << endl;

		//for (int i = 0; i < paths.size(); i++)
		//{
		//	double sumcost = 0;
		//	vector<int> outArray(pa.vpaths[0]);
		//	//将两条路径的边放在一个vector里
		//	outArray.insert(outArray.end(), paths[i].vpaths[i].begin(), paths[i].vpaths[i].end());
		//	sort(outArray.begin(), outArray.end()); ////去掉重复的边
		//	auto at = unique(outArray.begin(), outArray.end());
		//	/*	for (; at != outArray.end(); at++)
		//	{
		//		cout << *at << endl;
		//	}*/
		//	outArray.erase(at, outArray.end());
		//	
		//}
		bool ret = true;
		for (auto &it : f)
		{
			if (it > 0.21)
			{
				ret = false;
			}
			cout << it<<" " << endl;
		}
		return ret;

		/*	if(f<0.1)
		printf("sinlen=%ld,d1 %ld,len12 (%ld,%ld),min_len %ld repeat:  %.2f %% \n", outArray.size(), len1 + len2 - outArray.size(), len1, len2, min_len, f * 100.0);
		*/
	}

private:
	static bool minTimeCMP(const _Path &_start, const _Path &_end)
	{
		return _start.weight > _end.weight; // <升序排列
	}
	static bool minDistanceCMP(const _Path &_start, const _Path &_end)
	{
		return _start.distance < _end.distance; //默认的升序排列
	}

public:
	void minTimeSort()
	{
		sort(paths.begin(), paths.end(), minTimeCMP);
	}
	void minDistanceSort()
	{
		sort(paths.begin(), paths.end(), minDistanceCMP);
	}
	void 	remove_edge()
	{
		for (int i = 0; i < paths[0].vpaths[0].size(); i++)
		{
		/*	auto iter = std::find(vecStu.begin(), vecStu.end(), target);
			if (iter != vecStu.end())
			{
				std::cout << "method3: find exists." << std::endl;
			}*/
		}
	}


	void calTimeDistance()
	{
		/*for (auto &it : paths)
		{
			double sumdis = 0;
			double sumtime = 0;
			// cout << icnt++ << " :";
			for (int i = 1; i < it.nodes.size(); i++)
			{
				Node n = it.nodes.at(i - 1);
				sumdis += n.distance(it.nodes.at(i));
				string checkname = n.nodeType + "," + it.nodes.at(i).nodeType;
				// cout << n.nodeType << "-";
				Edge eg = edges[checkname];
				sumtime += eg.calTime();
			}
			// cout << "s0: \r\ntime=" << sumtime << "  dis:" << sumdis << endl;
			it.distance = sumdis;
			it.time = sumtime;
		}*/
	}
};
vector<string> split(const string &s, const string &seperator)
{
	vector<string> result;
	typedef string::size_type string_size;
	string_size i = 0;

	while (i != s.size())
	{
		//找到字符串中首个不等于分隔符的字母；
		int flag = 0;
		while (i != s.size() && flag == 0)
		{
			flag = 1;
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[i] == seperator[x])
				{
					++i;
					flag = 0;
					break;
				}
		}
		//找到又一个分隔符，将两个分隔符之间的字符串取出；
		flag = 0;
		string_size j = i;
		while (j != s.size() && flag == 0)
		{
			for (string_size x = 0; x < seperator.size(); ++x)
				if (s[j] == seperator[x])
				{
					flag = 1;
					break;
				}
			if (flag == 0)
				++j;
		}
		if (i != j)
		{
			result.push_back(s.substr(i, j - i));
			i = j;
		}
	}
	return result;
}
bool ReadJsonFromFile_edge(string strXmlPath, Graph &graph)
{

	std::string line, jsonStr;
	int maxid = 0;
	std::ifstream ifs(strXmlPath.c_str());
	while (getline(ifs, line))
	{
		jsonStr.append(line);
	}
	ifs.close();
	bool res;
	JSONCPP_STRING errs;
	Json::Value root, rows, data;
	Json::CharReaderBuilder readerBuilder;
	std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
	res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &root, &errs);
	if (!res || !errs.empty())
	{
		std::cout << "ReadJsonFromString error! " << errs << std::endl;
	}
	//for (int i = 0; i < root.size(); i++) //
	//{
	//	int nowid = root[i]["properties"]["osm_id"].asInt();
	//	if (maxid < nowid)
	//	{
	//		maxid = nowid;
	//	}
	//}
	//edge.resize(maxid+1);
	int node_size = 0;
	Node *plastnode;
	node_size = 0;
	graph.edge_max_num = 0;

	for (int i = 0; i < root.size(); i++)
	{
		graph.sum += root[i]["geometry"]["coordinates"].size(); // 得到"files"的数组个数
	}
	//cout << graph.sum << endl;
	graph.edgemap1 = (int *)malloc(sizeof(int) * (graph.sum));
	graph.edgemap2 = (int *)malloc(sizeof(int) * (graph.sum));
	graph.weight = (double *)malloc(sizeof(double) * (graph.sum));

	graph.pnodes = (Node *)malloc(sizeof(Node) * (graph.sum));
	for (int i = 0; i < graph.sum; i++)
	{
		graph.edgemap1[i] = 0xffffffff;
		graph.edgemap2[i] = 0xffffffff;
		graph.weight[i] = 1;
	}
	for (unsigned int i = 0; i < root.size(); i++) //
	{
		int j = 0;
		Edge *pEdge = new Edge;
		pEdge->roadname = i;
		pEdge->type = root[i]["type"].asString();
		pEdge->osm_id = root[i]["properties"]["osm_id"].asInt();
		if (maxid < pEdge->osm_id)
		{
			maxid = pEdge->osm_id;
		}
		pEdge->NAME = root[i]["properties"]["NAME"].asString();
		pEdge->EC = root[i]["properties"]["EC"].asString();
		pEdge->TYPE = root[i]["properties"]["TYPE"].asString();
		pEdge->WIDE = root[i]["properties"]["WIDE"].asString();
		pEdge->SPIDE = root[i]["properties"]["SPIDE"].asString();
		pEdge->LENGTH = root[i]["properties"]["LENGTH"].asString();
		pEdge->RTYPE = root[i]["properties"]["RTYPE"].asString();
		pEdge->LANE = root[i]["properties"]["LANE"].asDouble(); //直接省略小数后一位了
		pEdge->Shape_Leng = root[i]["properties"]["Shape_Leng"].asDouble();
		pEdge->geometry_type = root[i]["geometry"]["type"].asString();
		auto coordinates_size = root[i]["geometry"]["coordinates"].size(); // 得到"files"的数组个数
		Node *pNode = &graph.pnodes[node_size];							
		if (coordinates_size == 0)
		{
			continue;
		}
		else if (coordinates_size == 1)
		{
			printf("EEEEE1       \n");
			continue;
		}
		else if (coordinates_size == 2)
		{
			//printf("EEEEE2           \n");
			graph.edgemap1[graph.edge_max_num] = node_size;
			graph.edgemap2[graph.edge_max_num] = node_size + 1;

			pNode = &graph.pnodes[node_size]; //.print("");

			pNode->longitude = root[i]["geometry"]["coordinates"][j][0].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asFloat()); //setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asDouble());
			pNode->latitude = root[i]["geometry"]["coordinates"][j][1].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][1].asFloat());
			pNode->osm_id2 = pEdge->osm_id;
			pNode->osm_id2_2 = 0xffff;
			pNode->index = node_size;
			node_size += 1;

			j + 1;
			Node *pNode = &graph.pnodes[node_size]; //.print("");

			pNode->longitude = root[i]["geometry"]["coordinates"][j][0].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asFloat()); //setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asDouble());
			pNode->latitude = root[i]["geometry"]["coordinates"][j][1].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][1].asFloat());
			pNode->osm_id2 = pEdge->osm_id;
			pNode->osm_id2_2 = 0xffff;
			pNode->index = node_size;

			node_size += 1;

			//for (int i = graph.node_index-30; i < graph.node_index+2 /*graph.vnodes.size()*/; i++)
			//{
			//	printf("%d ", graph.edgemap1[i]);
			//}
			graph.edge_max_num += 1;
			continue;
		}
		for (j = 0; j < coordinates_size - 1; j++, node_size++, graph.edge_max_num++) // 遍历数组
		{
			graph.edgemap1[graph.edge_max_num] = node_size;
			graph.edgemap2[graph.edge_max_num] = node_size + 1;

			Node *pNode = &graph.pnodes[node_size]; //.print("");

			pNode->longitude = root[i]["geometry"]["coordinates"][j][0].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asFloat()); //setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asDouble());
			pNode->latitude = root[i]["geometry"]["coordinates"][j][1].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][1].asFloat());
			pNode->osm_id2 = pEdge->osm_id;
			pNode->osm_id2_2 = 0xffff;
			pNode->index = node_size;
		}

		pNode = &graph.pnodes[node_size];												   //.print("");
		pNode->longitude = root[i]["geometry"]["coordinates"][j][0].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asFloat()); //setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asDouble());
		pNode->latitude = root[i]["geometry"]["coordinates"][j][1].asDouble(); // setDecimalpoint(root[i]["geometry"]["coordinates"][j][1].asFloat());
		pNode->osm_id2 = pEdge->osm_id;
		pNode->osm_id2_2 = 0xffff;
		pNode->index = node_size;
		node_size += 1;
		//nodes[last_id].print("");
		//	edge.push_back(*pEdge);
	}
	//for (int i = 0; i < graph.sum /*graph.vnodes.size()*/; i++)
	//{
	//	printf("%d ", graph.edgemap1[i]);
	//}
	//printf("\n\n ");

	for (int V = 0; V < graph.edge_max_num /*graph.vnodes.size()*/; V++)
	{
			//printf("%d,%d\n", graph.edgemap1[V], graph.edgemap2[V]);
			Node *p1 = &graph.pnodes[graph.edgemap1[V]];//V为边序号ID graph.edgemap1[V]值为节点ID
			Node *p2 = &graph.pnodes[graph.edgemap2[V]];
			graph.weight[V] = p1->GetDistance(p2);
			string name(to_string(graph.edgemap1[V]) + "-" + to_string(graph.edgemap2[V]));
			graph.allEdgeswight[name] = graph.weight[V];
	}

	return true;
}

bool ReadJsonFromFile_point(string strXmlPath, Graph &graph)
{
	std::string line, jsonStr;
	std::ifstream ifs(strXmlPath.c_str());
	while (getline(ifs, line))
	{
		jsonStr.append(line);
	}
	ifs.close();
	bool res;
	JSONCPP_STRING errs;
	Json::Value root, rows, data;
	Json::CharReaderBuilder readerBuilder;
	std::unique_ptr<Json::CharReader> const jsonReader(readerBuilder.newCharReader());
	res = jsonReader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &root, &errs);
	if (!res || !errs.empty())
	{
		std::cout << "ReadJsonFromString error! " << errs << std::endl;
	}

	int countpoint = 0;

	double det = 1e-2;
	size_t zero = graph.sum;
	vector<Node> nodes_point;
	for (int i = 0; i < root.size(); i++) //
	{
		Node *pNode = new Node;
		pNode->longitude = (root[i]["geometry"]["coordinates"][0]).asDouble(); // setDecimalpoint((); //setDecimalpoint(root[i]["geometry"]["coordinates"][j][0].asDouble());
		pNode->latitude = (root[i]["geometry"]["coordinates"][1]).asDouble(); //setDecimalpoint(root[i]["geometry"]["coordinates"][1].asDouble());
		pNode->osm_id2 = root[i]["properties"]["osm_id"].asInt();						  // root[i]["geometry"]["coordinates"][j][0].asInt();
		pNode->osm_id2_2 = root[i]["properties"]["osm_id_2"].asInt();					  // root[i]["geometry"]["coordinates"][j][1].asInt();
		pNode->index = zero + i;
		if (pNode->osm_id2 == pNode->osm_id2_2)
			continue;
		vector<double> dis1;
		vector<double> dis2;
		vector<int> sdis1;
		vector<int> sdis2;
		for (int i = 0; i < zero; i++)
		{
			if (graph.pnodes[i].osm_id2 == pNode->osm_id2)
			{
				double d1 = graph.pnodes[i].calZDis(*pNode);
				//pNode->print("");
				dis1.push_back(d1);
				sdis1.push_back(i);
			}
			if (graph.pnodes[i].osm_id2 == pNode->osm_id2_2)
			{
				double d1 = graph.pnodes[i].calZDis(*pNode);
				dis2.push_back(d1);
				sdis2.push_back(i);
			}
		}

		vector<size_t> v1_index;
		vector<double> dis1_sort;
		vector<size_t> v2_index;
		vector<double> dis2_sort;
		//copy(dis1.begin(), dis1.end(), ostream_iterator<float>(cout, " ")); cout << endl; //输出vector中的内容

		v1_index = sort_indexes_e(dis1); //注意vec中的内容不变，不是返回排序后的向量
		v2_index = sort_indexes_e(dis2); //注意vec中的内容不变，不是返回排序后的向量

		for (int i = 0; i < dis1.size(); i++)
		{
			dis1_sort.push_back(dis1[v1_index[i]]);
		}
		for (int i = 0; i < dis2.size(); i++)
		{
			dis2_sort.push_back(dis2[v2_index[i]]);
		}

		if (dis1_sort[0] <= det && dis2_sort[0] <= det) //同时存在
		{
			Node *node_1 = &graph.pnodes[sdis1[v1_index[0]]];
			Node *node_2 = &graph.pnodes[sdis2[v2_index[0]]];

			node_1->endNode.insert(node_1->endNode.end(), node_2->endNode.begin(), node_2->endNode.end());
			node_1->osm_id2_2 = pNode->osm_id2_2;
			node_2->osm_id2_2 = pNode->osm_id2;
			node_2->endNode.insert(node_2->endNode.end(), node_1->endNode.begin(), node_1->endNode.end());
			//copy(sdis1.begin(), sdis1.end(), ostream_iterator<int>(cout, " ")); cout << endl; //输出vector中的内容
			//copy(v1_index.begin(), v1_index.end(), ostream_iterator<int>(cout, " ")); cout << endl; //输出vector中的内容

			auto iter_end = std::unique(node_2->endNode.begin(), node_2->endNode.end());
			node_2->endNode.erase(iter_end, node_2->endNode.end());

			for (int jm = 0; jm < graph.sum; jm++)
			{
				if (graph.edgemap1[jm] == sdis2[v2_index[0]])
				{
					graph.edgemap1[jm] = sdis1[v1_index[0]];
				}
				if (graph.edgemap2[jm] == sdis2[v2_index[0]])
				{
					graph.edgemap2[jm] = sdis1[v1_index[0]];
				}
			}
			graph.repect_point.push_back(sdis1[v1_index[0]]);
			//node_2->attr=ATTR_REMOVE;
			//pNode->print("");
			//node_1->print("");
			//node_2->print("");
			//nodes.erase(nodes.begin() + sdis2[v2_index[0]]);
			//printf("%d,%d\n", sdis1[v1_index[0]], sdis2[v2_index[0]]);
		}
		else
		{
			//->print("");
			//printf("%f,%f,%f,%f\n", dis1_sort[0], dis1_sort[1], dis2_sort[0], dis2_sort[1]);
			/*for (auto en : first->nodes)
			{
				double d1 = nodes[en].calZDis(*pNode);
				nodes[en].print("");

			}*/
			//copy(dis1_sort.begin(), dis1_sort.end(), ostream_iterator<double>(cout, " ")); cout << endl; //输出vector中的内容
		}
	}
	//for (int jm = 0; jm < graph.sum; jm++)
	//{
	//
	//	printf("%d ",	graph.edgemap1[jm]);
	//
	//}
	return true;
}
bool readXml(string strXmlPath, map<string, Node> &node, map<string, Edge> &edge, int &nodeNum, int &edgeNum)
{
	/*Node *pNode = new Node;
	Edge *pEdge = new Edge;

	string temp_post, temp_post_x, temp_post_y;
	string temp_start2end, temp_start, temp_end;

	//Load Xml file
	TiXmlDocument *Document = new TiXmlDocument();
	if (!Document->LoadFile(strXmlPath.c_str()))
	{
		cout << "Can not load Xml file" << endl;
		cin.get();
		return false;
	}

	//RootElement
	TiXmlElement *RootElement = Document->RootElement();
	//RootElement's FirstChildElement
	TiXmlElement *NextElement = RootElement->FirstChildElement();
	//Terminate loop until data fetch is end
	while (NextElement != NULL)
	{
		if (NextElement->ValueTStr() == "node")
		{
			nodeNum += 1;
			TiXmlElement *NodeElement = NextElement->FirstChildElement();

			//Assign nodeType
			while (NodeElement->ValueTStr() != "nodeType")
			{
				NodeElement = NodeElement->NextSiblingElement();
			}
			pNode->nodeType = NodeElement->GetText();

			//Assign node's position
			while (NodeElement->ValueTStr() != "post")
			{
				NodeElement = NodeElement->NextSiblingElement();
			}

			//Covert NodeElement's text to int array
			temp_post = NodeElement->GetText();
			int index = temp_post.find(',');
			char *currentIndex = (char *)temp_post.data();
			for (int i = 0; i < index; i++)
			{
				temp_post_x += *currentIndex;
				currentIndex++;
			}
			currentIndex = (char *)temp_post.data() + (index + 1);
			for (int i = index + 1; i < temp_post.size(); i++)
			{
				temp_post_y += *currentIndex;
				currentIndex++;
			}
			pNode->post[0] = stoi(temp_post_x);
			pNode->post[1] = stoi(temp_post_y);
			temp_post_x = "";
			temp_post_y = "";

			while (NodeElement->ValueTStr() != "nodeColor")
			{
				NodeElement = NodeElement->NextSiblingElement();
			}
			string strRGB = NodeElement->GetText();
			vector<string> ret = split(strRGB, ",");

			pNode->ViewRGB[0] = atof(&(ret[0])[0]);
			pNode->ViewRGB[1] = atof(&(ret[1])[0]);
			pNode->ViewRGB[2] = atof(&(ret[2])[0]);
			// printf("%f,%f,%f\r\n",pNode->ViewRGB[0],pNode->ViewRGB[1],pNode->ViewRGB[2]);
			node[pNode->nodeType] = *pNode;
			//node.push_back(*pNode);
		}
		if (NextElement->ValueTStr() == "edge1")
		{
			edgeNum += 1;
			TiXmlElement *EdgeElement = NextElement->FirstChildElement();

			//Assign edgeType
			while (EdgeElement->ValueTStr() != "edgeType")
			{
				EdgeElement = EdgeElement->NextSiblingElement();
			}
			pEdge->edgeType = EdgeElement->GetText();

			//Assign edge's start and terminus
			while (EdgeElement->ValueTStr() != "start2end")
			{
				EdgeElement = EdgeElement->NextSiblingElement();
			}
			//Covert NodeElement's text to int array
			temp_start2end = EdgeElement->GetText();
			int index = temp_start2end.find(',');
			char *currentIndex = (char *)temp_start2end.data();
			for (int i = 0; i < index; i++)
			{
				temp_start += *currentIndex;
				currentIndex++;
			}
			currentIndex = (char *)temp_start2end.data() + (index + 1);
			for (int i = index + 1; i < temp_start2end.size(); i++)
			{
				temp_end += *currentIndex;
				currentIndex++;
			}
			pEdge->start2end[0] = temp_start;
			pEdge->start2end[1] = temp_end;
			temp_start = "";
			temp_end = "";

			//Assign edge's weight
			while (EdgeElement->ValueTStr() != "weight")
			{
				EdgeElement = EdgeElement->NextSiblingElement();
			}
			pEdge->weight = atof(EdgeElement->GetText());
			pEdge->startNode = node[pEdge->start2end[0]];
			pEdge->endNode = node[pEdge->start2end[1]];
			pEdge->calDistance();
			//cout<<pEdge->distance<<endl;
			string end2start = pEdge->start2end[1] + "," + pEdge->start2end[0];
			//cout<<end2start<<"  "<<temp_start2end<<endl;
			edge[end2start] = *pEdge;
			// edge.push_back(*pEdge);
		}
		//Read next node in same layer
		NextElement = NextElement->NextSiblingElement();
	}

	//free memory
	delete pNode;
	delete pEdge;
	delete Document;
	cout << "Read file completely" << endl;
	*/
	return true;
}
//XML 添加属性
int writeXmlFile(string strXmlPath)
{
	/*TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针

	if (!writeDoc->LoadFile(strXmlPath.c_str()))
	{
		cout << "Can not load111 Xml file" << endl;
		cin.get();
		return false;
	}

	//文档格式声明
	// TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
	// writeDoc->LinkEndChild(decl); //写入文档

	int n = 3; //父节点个数

	TiXmlElement *RootElement = writeDoc->RootElement(); //根元素
	//writeDoc->LinkEndChild(RootElement);
	TiXmlElement *NextElement = RootElement->FirstChildElement();
	srand(time(NULL));

	while (NextElement != NULL)
	//n个父节点
	{
		if (NextElement->ValueTStr() == "edge1")
		{

			TiXmlElement *EdgeElement = NextElement->FirstChildElement();

			//姓名
			TiXmlElement *nameElement = new TiXmlElement("probability");
			NextElement->LinkEndChild(nameElement);
			float a = rand() % 10 / 100.0f;
			char str[128];
			sprintf_s(str, "%.2f", a);
			TiXmlText *nameContent = new TiXmlText(str);
			nameElement->LinkEndChild(nameContent);

			cout << EdgeElement->GetText() << endl;
		}
		// TiXmlElement *StuElement =NextElement->FirstChildElement();//Stu

		// NextElement->LinkEndChild(StuElement);//父节点写入文档

		// //分数
		// TiXmlElement *scoreElement = new TiXmlElement("score");
		// StuElement->LinkEndChild(scoreElement);

		// TiXmlText *scoreContent = new TiXmlText("88");
		// scoreElement->LinkEndChild(scoreContent);
		NextElement = NextElement->NextSiblingElement();
	}
	writeDoc->SaveFile("map_node50_v1.1.xml");
	delete writeDoc;
	*/
	return 1;
}

// void generateGraph(vector<Node> &node, vector<Edge> &edge, vector<Graph> &graph, int &nodeNum, int &edgeNum)
// {
// 	Graph *currentGraph = new Graph;
// 	/*currentGraph->vertex_num = nodeNum;
// 	currentGraph->edge_num = edgeNum;*/
// 	for (int i = 0; i < nodeNum; i++)
// 	{
// 		currentGraph->vertex = node[i].nodeType;
// 		currentGraph->post[0] = node[i].post[0];
// 		currentGraph->post[1] = node[i].post[1];
// 		for (int j = 0; j < edgeNum; j++)
// 		{
// 			if (currentGraph->vertex == edge[j].start2end[0])
// 			{
// 				currentGraph->graphEdges.push_back(edge[j].start2end[1]);
// 				currentGraph->edgeTypes.push_back(edge[j].edgeType);
// 				currentGraph->weights.push_back(edge[j].weight);
// 			}
// 			else
// 			{
// 				continue;
// 			}
// 		}
// 		graph.push_back(*currentGraph);
// 		//clear vector
// 		currentGraph->edgeTypes.erase(currentGraph->edgeTypes.begin(), currentGraph->edgeTypes.end());
// 		currentGraph->graphEdges.erase(currentGraph->graphEdges.begin(), currentGraph->graphEdges.end());
// 		currentGraph->weights.erase(currentGraph->weights.begin(), currentGraph->weights.end());
// 		/*vector<string>().swap(currentGraph->edgeType);
// 		vector<string>().swap(currentGraph->edge_dst);*/
// 	}
// 	delete currentGraph;
// }
bool readXml(string strXmlPath, vector<Node> &node, vector<Edge> &edge, int &nodeNum, int &edgeNum);

void generateGraph(vector<Node> &node, vector<Edge> &edge, vector<Graph> &graph, int &nodeNum, int &edgeNum);
